package com.ychs.controller;

import com.ychs.bean.Menu;
import com.ychs.bean.Role;
import com.ychs.bean.RoleMenu;
import com.ychs.service.*;
import net.sf.json.JSONArray;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.List;


/**
 * Description: 描述【角色操作】
 * Copyright: Copyright (c) 2018
 * Company: 英才汇硕信息技术有限公司
 *
 * @author tianchuanjie
 * @version 1.0
 * @created 2019/1/1 11:10
 */
@Controller
public class RoleInfoController {

    private Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private IRoleService roleService;


    @Autowired
    private IRoleMenuService roleMenuService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IMenuService menuService;

    /**
     * 角色jsp
     *
     * @param request
     * @return
     */
    @GetMapping("/roleInfo")
    public String roleInfo(HttpServletRequest request) {
        //查询所有角色信息
        List<Role> roleList = roleService.query();
        request.setAttribute("roleList", roleList);
        return "/admin/roleInfo";
    }


    @GetMapping("/addRoleInfo")
    public String RoleInfo() {
        return "/admin/roleZtree";
    }

    /**
     * 新增角色信息
     *
     * @param role     角色信息
     * @param node     菜单节点的组合字符串
     * @param response
     * @throws Exception
     */
    @GetMapping("/addRole1")
    public void addRoleInfo(Role role, String node, HttpServletResponse response) throws Exception {
        response.setCharacterEncoding("UTF-8");
        //增加角色信息
        boolean result = roleService.insert(role);
        String roleName = role.getRoleName();

        //根据角色名称进行查询 得到角色id
        Role role1 = roleService.selectRoleId(roleName);

        int roleId = role1.getRoleId();

        //将页面传来的节点【object object】类型的数据分割
        String menu_Id[] = node.split(",");

        //遍历节点  即菜单id
        for (int i = 1; i < menu_Id.length; i++) {
            //中间表增加 角色和菜单关系
            String menuId = menu_Id[i];

            if (menuId != null) {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(roleId);
                roleMenu.setMenuId(Integer.parseInt(menuId));
                boolean result1 = roleMenuService.insert(roleMenu);
            }
        }
        response.getWriter().print("增加角色成功");
    }

    /**
     * 同过角色id删除角色信息
     *
     * @param roleId 角色id
     * @return
     */
    @GetMapping("delectRole")
    public String delectRole(int roleId) {

        //删除角色  一条信息
        int result = roleService.deleteRole(roleId);
        //删除用户 多条信息
        int result2 = userService.deleteUserRole(roleId);
        //删除角色对应的菜单  多条信息
        int result1 = roleMenuService.deleteRole(roleId);
        return "redirect:roleInfo";
    }

    ;

    /**
     * 查看角色信息
     *
     * @param roleName 角色名称
     * @param roleId   角色id
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/updateRoleMenu")
    public String updateRoleMenu(String roleName, int roleId, HttpServletRequest request, HttpServletResponse response) {
        response.setCharacterEncoding("UTF-8");
        request.setAttribute("roleName", roleName);
        request.setAttribute("roleId", roleId);

        //查询所有菜单
        List<Menu> menuList = menuService.query();
        //查询该角色拥有的菜单
        List<RoleMenu> roleMenuList = roleMenuService.queryByRoleId(roleId);

        for (Menu menu : menuList) {
            for (RoleMenu roleMenu : roleMenuList) {
                if (menu.getMenuId() == roleMenu.getMenuId()) {
                    //通过checked属性给ztree勾选 页面显示
                    menu.setChecked(true);
                }
            }
        }
        JSONArray jsonArray = JSONArray.fromObject(menuList);
        request.setAttribute("jsonArray", jsonArray);

        return "/admin/roleMenuZtree";
    }

    /**
     * 修改角色信息
     *
     * @param role     角色对象信息
     * @param node     角色对应的菜单节点组合字符串
     * @param response
     */
    @GetMapping("/updateRole")
    public void updataRole(Role role, String node, HttpServletResponse response) {
        response.setCharacterEncoding("UTF-8");

        //更改角色名
        int result = roleService.updateRole(role);
        //更改角色对应的菜单

        //先删除中间表的角色对应菜单  再将现在的node 增加进数据库中
        int result1 = roleMenuService.deleteRole(role.getRoleId());
        //分割节点信息
        String menu_Id[] = node.split(",");

        //遍历节点信息 增加菜单节点和角色信息
        for (int i = 1; i < menu_Id.length; i++) {

            String menuId = menu_Id[i];

            if (menuId != null) {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(role.getRoleId());
                roleMenu.setMenuId(Integer.parseInt(menuId));
                boolean result3 = roleMenuService.insert(roleMenu);
            }
        }
        try {
            //响应页面
            response.getWriter().print("角色信息修改成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
